﻿//********************************************************************************//
//创建日期：<创建日期，2016-02-26>
//创建作者：<杨学良>
//功能说明：增回灌表
//modify:郭登杰
//********************************************************************************//
using System;
using Entity;
using BLL;
using BLL.Access;
using System.Data;
using Web.Class;

namespace Web.Pages.YongShui.YongHuGL
{
    public partial class ZengHuiGB : BasePage
    {
        #region 属性
        
        /// <summary>
        /// 当前操作项ID
        /// </summary>
        private object YONGHUBHID
        {
            get
            {
                return ViewState["_YONGHUBHID"];
            }
            set
            {
                ViewState["_YONGHUBHID"] = value;
            }
        }

        /// <summary>
        /// 当前增加的水表ID(当增表时水表号已经存在时,记录)
        /// </summary>
        private int SHUIBIAOID
        {
            get
            {
                return Convert.ToInt32(ViewState["_SHUIBIAOID"]);
            }
            set
            {
                ViewState["_SHUIBIAOID"] = value;
            }
        }

        /// <summary>
        /// 记录查询结果集
        /// </summary>
        private DataTable dtSource
        {
            get
            {
                return ViewState["dtSource"] as DataTable;
            }
            set
            {
                ViewState["dtSource"] = value;
            }
        }

        /// <summary>
        /// 当前操作用户站点
        /// </summary>
        private string ZHANDIANBH
        {
            get
            {
                return Convert.ToString(ViewState["_ZHANDIANBH"]);
            }
            set
            {
                ViewState["_ZHANDIANBH"] = value;
            }
        }

        #endregion

        #region 变量

        /// <summary>
        /// 水表信息对象
        /// </summary>
        YS_CB_SHUIBIAOXX _YS_CB_SHUIBIAOXX = new YS_CB_SHUIBIAOXX();

        /// <summary>
        /// 用户信息BLL
        /// </summary>
        YS_YG_YONGHUXXBLL _YS_YG_YONGHUXXBLL = new YS_YG_YONGHUXXBLL();

        /// <summary>
        /// 用户水表关系BLL
        /// </summary>
        YS_CB_YONGHUSBGXBLL _YS_CB_YONGHUSBGXBLL = new YS_CB_YONGHUSBGXBLL();

        /// <summary>
        /// 水表信息BLL
        /// </summary>
        YS_CB_SHUIBIAOXXBLL _YS_CB_SHUIBIAOXXBLL = new YS_CB_SHUIBIAOXXBLL();

        /// <summary>
        /// 日志BLL
        /// </summary>
        SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        #endregion

        #region 事件
        
        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitControl();
                InitData();                
                Display();                
            }
            this.gvShuiBiao.DataSource = this.dtSource;
            this.gvShuiBiao.DataBind();
        }

        /// <summary>
        /// 查找数据
        /// </summary>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            InitData();
            SetData();
            this.Status = GridViewStatus.Search;
            LoadData();
            Display();
        }

        /// <summary>
        /// 添加操作
        /// </summary>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            InitData();
            if(ValidateData(true))
            {
                this.Status = GridViewStatus.Add;
                Display();
            }
        }

        /// <summary>
        /// 放弃操作
        /// </summary>
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.Status = GridViewStatus.Search;
            Display();
        }

        /// <summary>
        /// 保存操作
        /// </summary>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidateData(false))
            {
                SaveData();
            }
        }
     
        #endregion

        #region 方法

        /// <summary>
        /// 初始化控件
        /// </summary>
        private void InitControl()
        {
            //绑定行政区
            WordsManager manger = new WordsManager();
            PageTools.BindComboBox(ddlXingZhengQ,BusinessTool.GetTextAndValue("YS_JX_XINGZHENGQ","S_MINGCHENG","S_BIANHAO","-","I_JLZT=0").Tables[0]);
            ddlXingZhengQ.SelectedIndex = 0;
            //绑定水表类型
            PageTools.BindComboBox(ddlShuiBiaoLX,manger.Instance.FindWordsDataTableWithMiddleAffixByBelongID(5,"-"));
            ddlShuiBiaoLX.SelectedIndex = 0;
            dtZhuangBiaoRQ.Value = DateTime.Now;
            //根据操作人加载管理部门(站点)
            DataTable dtZhanDian = BusinessTool.GetTextAndValue("View_AS_UserInfo", "ZHANDIANMC", "ZHANDIANBH", "-", string.Format("USERID={0}", LoginInfo.CurrentUser.UserID)).Tables[0];
            this.ZHANDIANBH = dtZhanDian.Rows[0]["VALUE"].ToString();
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {            
            txtBiaoCe.Text = "";
            txtBiaoXing.Text = "";
            txtKouJing.Text = "";
            txtShuiBiaoDZ.Text = "";
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        private void LoadData()
        {
            //查询用户下的水表
            DataRowCollection drc = _YS_YG_YONGHUXXBLL.SelectYS_YG_YONGHUXXesDynamic(string.Format("I_LEIXING=1 and S_YONGHUH='{0}' {1}", TxtYongHuH.Text.Trim().Replace("'", "''"), this.ZHANDIANBH == "00" ? "" : " and S_ZHANDIANBH='" + this.ZHANDIANBH + "'"), null).Rows;
            if (drc.Count > 0)
            {
                TxtYongHuM.Text = drc[0]["S_YONGHUM"].ToString();
                TxtDiZhi.Text = drc[0]["S_DIZHI"].ToString();
                YONGHUBHID = drc[0]["ID"].ToString();
                string sWhere = "'---------'";
                foreach (DataRow dr in _YS_CB_YONGHUSBGXBLL.SelectYS_CB_YONGHUSBGXesDynamic(string.Format("I_LEIXING=1 and S_YONGHUBH='{0}' and I_SHUIBIAOZT=0", TxtYongHuH.Text.Trim().Replace("'", "''")), null).Rows)
                {
                    sWhere += ",'" + dr["S_SHUIBIAOBH"].ToString() + "'";
                }
                this.dtSource= _YS_CB_SHUIBIAOXXBLL.SelectYS_CB_SHUIBIAOXXesDynamic(String.Format("I_LEIXING=1 and S_SHUIBIAOBH in ({0})", sWhere), null);
                this.Status = GridViewStatus.Search;
                gvShuiBiao.DataSource = this.dtSource;
                gvShuiBiao.DataBind();
            }
            else
            {
                PageTools.AlertMsg(this, "该用户不存在");
                this.Status = GridViewStatus.Init;
            }
        }

        /// <summary>
        ///用于控制添加、修改、查看三种状态的显示
        /// </summary>
        private void Display()
        {
            switch (this.Status)
            {
                case GridViewStatus.Init:
                    btnAdd.Visible = true;
                    divEdit.Visible = false;
                    SetText(false);
                    break;
                case GridViewStatus.Search:
                    btnAdd.Visible = true;
                    divEdit.Visible = false;
                    SetText(false);
                    break;
                case GridViewStatus.Add:
                    btnAdd.Visible = false;
                    divEdit.Visible = true;
                    SetText(true);
                    break;
            }
        }
        
        /// <summary>
        /// 设置输入框状态
        /// </summary>
        private void SetText(bool b)
        {
           
        }

        /// <summary>
        /// 给输入框赋值
        /// </summary>
        private void SetData()
        {

        }

        /// <summary>
        /// 保存数据
        /// </summary>
        private void SaveData()
        {
            //当前无操作的数据则返回
            if (this.Status == GridViewStatus.Init)
            {
                return;
            }
            //判断用户号是否存在,不存在返回
            DataRowCollection dtTemp = _YS_YG_YONGHUXXBLL.SelectYS_YG_YONGHUXXesDynamic(string.Format("I_JLZT=0 AND I_LEIXING=1 AND S_YONGHUH='{0}'",this.TxtYongHuH.Text),null).Rows;
            if (dtTemp.Count==0)
            {
                PageTools.AlertMsg(this, "该用户不存在！");
                return;
            }
            if (this.SHUIBIAOID > 0)
            {
                this._YS_CB_SHUIBIAOXX = this._YS_CB_SHUIBIAOXXBLL.SelectYS_CB_SHUIBIAOXX(this.SHUIBIAOID);
            }
            else
            {
                this.SHUIBIAOID = 0;
            }
            this._YS_CB_SHUIBIAOXX.I_TYPE = Convert.ToInt32(ddlShuiBiaoLX.Value);
            this._YS_CB_SHUIBIAOXX.S_GUANLIKBH = ddlXingZhengQ.Value.ToString();
            this._YS_CB_SHUIBIAOXX.S_DIZHI = txtShuiBiaoDZ.Text;
            this._YS_CB_SHUIBIAOXX.I_KOUJING = Convert.ToInt32(txtKouJing.Text.Trim());
            this._YS_CB_SHUIBIAOXX.D_ZHUANGBIAO = (DateTime)dtZhuangBiaoRQ.Value;
            this._YS_CB_SHUIBIAOXX.S_CH = txtBiaoCe.Text;
            this._YS_CB_SHUIBIAOXX.S_BIAOXING = this.txtBiaoXing.Text;
            this._YS_CB_SHUIBIAOXX.S_ZHANDIANBH = this.ZHANDIANBH;
            this._YS_CB_SHUIBIAOXX.S_SHUIBIAOBH = TxtShuiBiaoH.Text;
            this._YS_CB_SHUIBIAOXX.S_MINGCHENG = "";
            this._YS_CB_SHUIBIAOXX.I_NO = 0;
            this._YS_CB_SHUIBIAOXX.I_JLZT = 0;
            this._YS_CB_SHUIBIAOXX.I_LEIXING = 1;
            this._YS_CB_SHUIBIAOXX.D_CHAOBIAO = null;
            this._YS_CB_SHUIBIAOXX.D_SHANGYUE = null;
            this._YS_CB_SHUIBIAOXX.I_BENYUECM = null;
            this._YS_CB_SHUIBIAOXX.I_BENYUESL = null;
            this._YS_CB_SHUIBIAOXX.I_CHAOBIAOZQ = null;
            this._YS_CB_SHUIBIAOXX.I_CHAOBIAOZT = null;
            this._YS_CB_SHUIBIAOXX.I_GUJICS = null;
            this._YS_CB_SHUIBIAOXX.I_JIAODUISL = null;
            //获取水量年月
            int ym=Convert.ToInt32(BusinessTool.YS_GETSETTINGSVALUE(eSETTINGS_KEYNAME.WATER_Y_M.ToString()));
            this._YS_CB_SHUIBIAOXX.I_M = ym%100; ;
            this._YS_CB_SHUIBIAOXX.I_SHANGYUECM = null;
            this._YS_CB_SHUIBIAOXX.I_SHANGYUESL = null;
            this._YS_CB_SHUIBIAOXX.I_Y = ym/100;
            this._YS_CB_SHUIBIAOXX.N_YSJ = 0;
            this._YS_CB_SHUIBIAOXX.S_BEIZHU = null;
            this._YS_CB_SHUIBIAOXX.S_CAOZUOR = LoginInfo.CurrentUser.Account;
            this._YS_CB_SHUIBIAOXX.S_JH = null;
            this._YS_CB_SHUIBIAOXX.S_KUAIHAO = null;
            if(_YS_CB_SHUIBIAOXXBLL.InsertOrUpdate(_YS_CB_SHUIBIAOXX))
            {
                YS_CB_YONGHUSBGX _YS_CB_YONGHUSBGX = new YS_CB_YONGHUSBGX();
                _YS_CB_YONGHUSBGX.S_ZHANDIANBH= this.ZHANDIANBH;
                _YS_CB_YONGHUSBGX.S_YONGHUBH = this.TxtYongHuH.Text;
                _YS_CB_YONGHUSBGX.S_SHUIBIAOBH =_YS_CB_SHUIBIAOXX.S_SHUIBIAOBH;
                _YS_CB_YONGHUSBGX.I_SHUIBIAOZT =0;
                //设置操作信息,删除状态,本月下月类别
                _YS_CB_YONGHUSBGX.S_XIUGAIR = LoginInfo.CurrentUser.Account;
                _YS_CB_YONGHUSBGX.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();                
                _YS_CB_YONGHUSBGX.I_JLZT = 0;
                _YS_CB_YONGHUSBGX.I_LEIXING = 1;
                //执行操作
                if (_YS_CB_YONGHUSBGXBLL.InsertOrUpdate(_YS_CB_YONGHUSBGX))
                {
                    new SpHelper().P_YSYG_UPDATEYHSBS(this.TxtYongHuH.Text,2);
                    LoadData();
                    this.Status = GridViewStatus.Search;
                    Display();
                }
                else
                {
                    PageTools.AlertMsg(this, "保存失败！");
                }
            }
        }


        /// <summary>
        /// 保存验证
        /// </summary>
        /// <returns></returns>
        private bool ValidateData(bool setInfo)
        {            
            if (TxtShuiBiaoH.Text.Trim() == "")
            {
                PageTools.AlertMsg(this, "水表号为空");
                this.TxtShuiBiaoH.Focus();
                return false;
            }
            else if (TxtShuiBiaoH.Text.Trim().Length != 9)
            {
                PageTools.AlertMsg(this, "水表号长度不对。");
                this.TxtShuiBiaoH.Focus();
                return false;
            }
            else if (TxtShuiBiaoH.Text.Trim()[2] != '9')
            {
                PageTools.AlertMsg(this, "水表号第三位必须为9。");
                this.TxtShuiBiaoH.Focus();
                return false;
            }
            else if (this._YS_CB_YONGHUSBGXBLL.SelectYS_CB_YONGHUSBGXesDynamic(String.Format("S_SHUIBIAOBH='{0}'", TxtShuiBiaoH.Text.Trim().Replace("'", "''")), null).Rows.Count > 0)
            {
                PageTools.AlertMsg(this, "该水表已被考核。");
                this.TxtShuiBiaoH.Focus();
                return false;
            }            
            else
            {
                if (setInfo)
                {
                    //判断是否已经存在该水表号,如果存在,则以该水表号加载对应信息
                    DataRowCollection drcTemp = this._YS_CB_SHUIBIAOXXBLL.SelectYS_CB_SHUIBIAOXXesDynamic(string.Format("I_LEIXING=1 AND S_SHUIBIAOBH='{0}'", this.TxtShuiBiaoH.Text), null).Rows;
                    if (drcTemp.Count > 0)
                    {
                        this.SHUIBIAOID = Convert.ToInt32(drcTemp[0]["ID"]);
                        this.txtBiaoCe.Text = Convert.ToString(drcTemp[0]["S_CH"]);
                        this.txtBiaoXing.Text = Convert.ToString(drcTemp[0]["S_BIAOXING"]);
                        this.txtShuiBiaoDZ.Text = Convert.ToString(drcTemp[0]["S_DIZHI"]);
                        this.txtKouJing.Text = Convert.ToString(drcTemp[0]["I_KOUJING"]);
                    }
                    else
                    {
                        this.SHUIBIAOID = -1;
                    }
                }
            }
            if (setInfo==false)
            {
                if (this.txtShuiBiaoDZ.Text == "")
                {
                    PageTools.AlertMsg(this, "请输入水表地址！");
                    this.txtShuiBiaoDZ.Focus();
                    return false;
                }
                else if (this.txtKouJing.Text == "")
                {
                    PageTools.AlertMsg(this, "请输入口径！");
                    this.txtKouJing.Focus();
                    return false;
                }
                else if (!PageTools.ValidateString(this.txtKouJing.Text,PageTools.eValidateType.数字))
                {
                    PageTools.AlertMsg(this, "请输入正确的数值！");
                    this.txtKouJing.Focus();
                    return false;
                }
                else if (this.txtBiaoCe.Text == "")
                {
                    PageTools.AlertMsg(this, "请输入册号！");
                    this.txtBiaoCe.Focus();
                    return false;
                }
            }
            return true;
        }

        #endregion
 
    }
}